package weaver.interfaces.expense.action.gzc;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.util.ExpenseCompare;
import weaver.interfaces.util.ExpenseCompareUtil;
import weaver.interfaces.util.ExpenseInfoUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
/**
 * 
 * @ClassName: YSKFWZLYZDS_ExpenseAction
 * @Description: 院所库房物资领用单-重点室
 * @author xiyufei
 * @date 2017-3-20 上午11:19:21 
 *
 */
public class YSKFWZLYZDS_ExpenseAction  extends BaseBean implements Action{

 
	public String execute(RequestInfo requestInfo) { 
		try {
			String workflowid = requestInfo.getWorkflowid();
			String requestid = requestInfo.getRequestid();
			writeLog("进入YSKFWZLYZDS_ExpenseAction-----------"+requestid);
			RecordSet rs = new RecordSet(); 
			rs.executeSql("select formid from workflow_base where id = " + workflowid);
			rs.next();
			String formid = rs.getString("formid");
			String formtable = "formtable_main_" + formid.replaceAll("-", "");
			ExpenseInfoUtil expenseInfoUtil = new ExpenseInfoUtil();
			String ccode = "";
			double amount = 0;
			String cexpenseitemcode = "";
			String citemcode = "";
			String iyear = "";
			String iskt ="0";
			//System.out.println("select  id,ks,cexpenseitemcode,citemcode,zje,zjly,sqrq,kth from "+formtable+" where requestid="+requestid);
			rs.execute("select id,b.ks,a.zcxm,a.je,b.zj,b.sqrq,b.kth2,b.sf from "+formtable+"_dt1 a , "+formtable+" b where a.mainid=b.id and  b.requestid="+requestid);
			while(rs.next()){   
				ccode =  expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks"))); 
				amount =  Util.getDoubleValue(rs.getString("je"),0); 
				cexpenseitemcode=  Util.null2String(rs.getString("zcxm"));  
				String kth =  Util.null2String(rs.getString("kth2"));  
				iyear =  Util.null2String(rs.getString("sqrq")).substring(0,4); 
				String sfkt = Util.null2String(rs.getString("sf")); 
				
				ExpenseCompareUtil  expenseCompareUtil= new  ExpenseCompare();
				if("0".equals(sfkt)){// 课题号+项目支出
					iskt ="1";
					if(!expenseCompareUtil.compareExpense2(workflowid, cexpenseitemcode,kth,amount,iyear)){
						requestInfo.getRequestManager().setMessageid("1111111111");
						requestInfo.getRequestManager().setMessagecontent("课题号:"+kth+" ,支出项目:"+expenseInfoUtil.getCItemName(cexpenseitemcode)+"的预算不足"); 
						return FAILURE_AND_CONTINUE;
					}
				}
				
				if("1".equals(sfkt)){//科室+支出项目
					if(!expenseCompareUtil.compareExpense4(workflowid,cexpenseitemcode,amount,iyear)){
						requestInfo.getRequestManager().setMessageid("1111111111");
						requestInfo.getRequestManager().setMessagecontent("支出项目:"+expenseInfoUtil.getCItemName(cexpenseitemcode)+"的预算不足"); 
						return FAILURE_AND_CONTINUE;
					}
				}
			}
			rs.beforFirst();
			while(rs.next()){  
				ccode =  expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks"))); 
				amount =  Util.getDoubleValue(rs.getString("je"),0); 
				cexpenseitemcode=  Util.null2String(rs.getString("zcxm"));  
				String kth =  Util.null2String(rs.getString("kth2"));  
				iyear =  Util.null2String(rs.getString("sqrq")).substring(0,4);  
				
				if(!expenseInfoUtil.subtractExpense(workflowid, ccode, cexpenseitemcode, citemcode, kth, amount, iyear,iskt)){
					requestInfo.getRequestManager().setMessageid("1111111111");
					requestInfo.getRequestManager().setMessagecontent("预算更新失败"); 
					return FAILURE_AND_CONTINUE;
				}
			}
		} catch (Exception e) {
			requestInfo.getRequestManager().setMessageid("1111111111");
			requestInfo.getRequestManager().setMessagecontent(e.getMessage()); 
			return FAILURE_AND_CONTINUE;
		}
	
		return SUCCESS;
	}

}
